/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package boundary.barCode;

import java.io.FileInputStream;
import java.io.IOException;

import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintException;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.SimpleDoc;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.Copies;



/**
 *
 * @author Juan
 */
public final class ImprimirCode {
    
    /*
     * 
     */
    
    public ImprimirCode(String cod) throws Exception{
        imprimir(cod);
    }
    
    
    public static void imprimir(String cod) {
        try {
            PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
            pras.add(new Copies(1));
            PrintService pss[] = PrintServiceLookup.lookupPrintServices(DocFlavor.INPUT_STREAM.PNG, pras);
            if (pss.length == 0) {
                    throw new RuntimeException("No se detecta impresora disponible.");
                }
            PrintService ps = pss[0];
            System.out.println("Imprimiendo en " + ps);
            DocPrintJob job = ps.createPrintJob();
            FileInputStream fin = new FileInputStream("src/CodigosGenerados/"+cod+".png");
            Doc doc = new SimpleDoc(fin, DocFlavor.INPUT_STREAM.PNG, null);
            try {
                job.print(doc, pras);
            } catch (PrintException ex) {
            }
            /*///////////////////////////////////////////////////////-----------------------
            PrintRequestAttributeSet prass = new HashPrintRequestAttributeSet();
            prass.add(new Copies(1));
            PrintService[] pss2=PrintServiceLookup.lookupPrintServices(DocFlavor.INPUT_STREAM.PNG, pras);
            PrintService ps2 = pss2[0];
            if (pss2.length == 0) {
                    throw new RuntimeException("No se detecta impresora disponible.");
                }
            String direccion="src/CodigosGenerados/Abc12345.png";
            Imprimible i= new Imprimible();
            
                
            PrinterJob jobb = (PrinterJob) ps2.createPrintJob();//PrinterJob.getPrinterJob();
            jobb.setPrintable( i);
            FileInputStream fin2 = new FileInputStream("src/CodigosGenerados/"+cod+".png");
            Doc doc2= new SimpleDoc(fin2, DocFlavor.INPUT_STREAM.PNG, null);
            DocPrintJob job3= (DocPrintJob) jobb;
            job3.print(doc2,pras);
            jobb.print();
        ///*
        try 
        {
           jobb.print();
        } 
        catch (PrinterException e) 
        {
           e.printStackTrace();
        }*/
        //////////////////////////////////////////////////////////-------------------------
            fin.close();
        } catch (IOException ex) {
            
        }
  
    
}
}
